package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.model.SpecModel;
import com.future.mayi.view.GoodsBatchItemView;
import com.future.mayi.widget.FlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsSkuDialog extends Dialog implements View.OnClickListener {

    @BindView(R.id.fl_goods)
    FlowLayout flGoods;

    private LayoutInflater mInflater;


    public GoodsSkuDialog(@NonNull Context context) {
        super(context, R.style.transparentDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_goods_sku, null);
        view.setMinimumWidth(CommonUtils.getScreenSize(context)[0]);
        setContentView(view);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        ButterKnife.bind(this);
        mInflater = LayoutInflater.from(context);
    }

    @OnClick(R.id.iv_close)
    public void onClick() {
        dismiss();
    }

    private GoodsBatchItemView itemView;

    public void show(GoodsBatchItemView itemView) {
        List<SpecModel> list = itemView.getList();
        if (list == null || list.size() == 0) return;
        super.show();
        this.itemView = itemView;
        flGoods.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            TextView tv = (TextView) mInflater.inflate(R.layout.layout_goods_sku_item, flGoods, false);
            tv.setText(list.get(i).getGoods_spec());
            tv.setTag(list.get(i));
            tv.setOnClickListener(this);
            flGoods.addView(tv);
        }
    }

    @Override
    public void onClick(View v) {
        SpecModel model = (SpecModel) v.getTag();
        itemView.addItem(model);
        flGoods.removeView(v);
    }
}
